﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CmdArgs.Settings;
using CmdArgs.Validation.Options;
using CmdArgs;
using System.ComponentModel;
using CmdArgs.Validation;

namespace Time
{
    class Program
    {
        static void Main(string[] args)
        {
            var settingsBuilder = new SettingsBuilder();

            settingsBuilder.AddOption("format", 'f', OptionParametersCount.Exactly(1))
                .SetDescription("Specify output format, possibly overriding the format specified in the environment variable TIME.")
                .SetParameterName("FORMAT")
                .SetDefaultValue("123")
                .AddValidator(new RegexValidator("^[0-9MYys]*$"));

            settingsBuilder.AddOption("output", 'o', OptionParametersCount.Exactly(1))
                .SetDescription("Do not send the results to stderr, but overwrite the specified file.")
                .SetParameterName("OUTPUT");

            settingsBuilder.AddFlag("portability", 'p')
                .SetDescription("Use the portable output format.");

            settingsBuilder.AddFlag("append", 'a')
                .SetDescription("(Used together with -o.) Do not overwrite but append.");

            settingsBuilder.AddFlag("verbose", 'v')
                .SetDescription("Give very verbose output about all the program knows about.");

            settingsBuilder.AddFlagWithLongName("help")
                .SetDescription("Print a usage message on standard output and exit successfully.");

            settingsBuilder.AddFlag("version", 'V')
                .SetDescription("Print version information on standard output, then exit successfully.");

            var parser = new CommandLineParser(settingsBuilder.ToSettings());

            var validationResult = parser.Validate(args);
            if (validationResult.IsValid == false)
            {
                validationResult.WriteErrorsToConsole();
                return;
            }


            var result = parser.Parse(args);

            if (result.Options.Contains("help"))
            {
                Console.WriteLine();
                Console.WriteLine("----------------------------");
                Console.WriteLine("time [options] command [arguments...]");
                Console.WriteLine();
                settingsBuilder.ToSettings().WriteHelpToConsole();
            }
            else if (result.Options.Contains("version"))
            {
                Console.WriteLine("Version: 3.21 Alphabeta");
            }
            else
            {
                if (result.Options.Contains("format"))
                {
                    Console.WriteLine("Format: {0}", result.Options["format"].Value);
                }

                if (result.Options.Contains("output"))
                {
                    Console.WriteLine("Output: {0}", result.Options["output"].Value);
                }

                Console.WriteLine("Other flags");
                foreach (var option in result.Options)
                {
                    if (option.Settings.LongNameAliases.First() == "format" ||
                        option.Settings.LongNameAliases.First() == "output")
                    {
                        continue;
                    }

                    Console.WriteLine("---");
                    option.Settings.WriteHelpToConsole();
                }

                Console.WriteLine("--------");
                Console.WriteLine("Ordinary parameters:");
                Console.WriteLine(string.Join(", ", result.Arguments.ToArray()));
            }
        }
    }
}
